package cn.iune.erp.accounting;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;
import com.justep.baas.data.Row;
import com.justep.baas.data.RowState;
import com.justep.baas.data.Table;
import com.justep.baas.data.Transform;

@Transactional
@Service
public class PeriodCaptionServiceImpl implements IPeriodCaptionService {

	@Autowired
	private IPeriodCaptionDao dao;
	
	@Override
	public PeriodCaption getByID(String fID) {
		return dao.getByID(fID);
	}

	@Override
	public void insert(PeriodCaption obj) {
		dao.insert(obj);
	}

	@Override
	public void delete(String fID) {
		dao.delete(fID);
	}
	
	/*
	在@Transaction注解中定义noRollbackFor和RollbackFor指定某种异常是否回滚。 
	@Transactional(noRollbackFor=RuntimeException.class) 
	@Transactional(RollbackFor=Exception.class) 
	*/
	@Transactional
	@Override
	public JSONObject save(JSONObject object) throws ParseException {
		//Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类)进行回滚。 
		//如果一个方法抛出Exception或者Checked异常，Spring事务管理默认不进行回滚。 
		// JSON转换Table
		System.out.println("PeriodCaptionServiceImpl.save-->>"+object.toJSONString());
		Table table= Transform.jsonToTable(object);
		
		JSONObject data = new JSONObject();
		data.put("concept", table.getTableName());
		JSONArray rows = new JSONArray();
		List<String> idsNew = new ArrayList<String>();
		List<String> idsDel = new ArrayList<String>();
				
		List<Row> list = table.getRows();
		for(Row row : list){
			JSONObject javaObject= Transform.rowToJavaObject(table, row);
			PeriodCaption obj = JSON.toJavaObject(javaObject, PeriodCaption.class);
			if(RowState.NEW.equals(obj.getRowState())){
				dao.insert(obj);
				idsNew.add(obj.getfID());
			}
			if(RowState.DELETE.equals(obj.getRowState())){
				dao.delete(obj.getfID());
				idsDel.add(obj.getfID());
			}
			if(RowState.EDIT.equals(obj.getRowState())){
				PeriodCaption objNew = dao.update(obj);
				//返回+1后的version到前台BizData中进行刷新，使得前台的version与后台的一致。
				///////////////////////////////////////////////////////////////////////
				JSONObject jsonRow = new JSONObject();
				jsonRow.put("id", obj.getfID());
				jsonRow.put("version", objNew.getVersion());
				rows.add(jsonRow);
				//////////////////////////////////////////////////////////////////////
			}
			//测试使用
			/*
			 * 测试结果表明，如果要更新的java对象与数据库中的数据一致，那么merge函数
			 * 被调用之后返回的version和被调用之前的version是相同的，即使merge之后
			 * 使用了flush（）函数也是一样的效果。
			 */
			/*
			if(RowState.NONE.equals(obj.getRowState())){
				PeriodCaption objNew = dao.update(obj);
				//返回+1后的version到前台BizData中进行刷新，使得前台的version与后台的一致。
				///////////////////////////////////////////////////////////////////////
				PeriodCaption objQuery = dao.getByID(obj.getfID());
				log.info("update更新后flush（）之后查询出objQuery.getVersion()-->>"+objQuery.getVersion());
				JSONObject jsonRow = new JSONObject();
				jsonRow.put("id", obj.getfID());
				jsonRow.put("version", objQuery.getVersion());
				rows.add(jsonRow);
				//////////////////////////////////////////////////////////////////////
			}
			*/
		}
		
		data.put("rows", rows);
		data.put("idsNew", idsNew);
		data.put("idsDel", idsDel);
		
		return data;
	}

	@Override
	public PeriodCaption update(PeriodCaption obj) {
		return dao.update(obj);
	}
	
	@Override
	public PeriodCaption getByNo(String yearNo, String periodNo, String captionNo, String enterpriseNo) {
		return dao.getByNo(yearNo, periodNo, captionNo, enterpriseNo);
	}
	
	@Override
	public List<PeriodCaption> getByPeriod(String yearNo, String periodNo,String enterpriseNo){
		return dao.getByPeriod(yearNo, periodNo, enterpriseNo);
	}
	
	//删除指定数据
	@Override
	public int deleteByPeriod(String yearNo, String periodNo,String enterpriseNo){
		dao.deleteByPeriod(yearNo, periodNo, enterpriseNo);
		return 0;
	}
	
	@Override
	public JSONObject newData(JSONObject data) {
		return dao.create(data);
	}
	
	@Override
	public JSONObject deleteData(JSONObject param) {
		JSONArray ids = param.getJSONArray("ids");
		for(int i=0;i<ids.size();i++){
			String id = ids.getString(i);
			//这里可以增加是否可删除的判断
			
			dao.delete(id);
		}

		JSONObject data = new JSONObject();
		data.put("ids", ids);
		
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", data);
		
		return json;
	}

	/*
	在@Transaction注解中定义noRollbackFor和RollbackFor指定某种异常是否回滚。 
	@Transactional(noRollbackFor=RuntimeException.class) 
	@Transactional(RollbackFor=Exception.class) 
	*/
	@Transactional
	@Override
	public JSONObject saveData(JSONObject param) {
		//Spring的事务管理默认只对出现运行期异常(java.lang.RuntimeException及其子类)进行回滚。 
		//如果一个方法抛出Exception或者Checked异常，Spring事务管理默认不进行回滚。 
		// JSON转换Table
		//System.out.println(object);
		JSONObject data;
		try {
			data = this.save(param);
					
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", data);
			
			return json;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			
			return json;
		} 
	}
	
	
	/* (non-Javadoc)
	 * @see com.iune.erp.baseinfo.aaa#queryJDBC(com.alibaba.fastjson.JSONObject)
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public JSONObject queryData(JSONObject param){
		//在此层面（ServiceImpl）统一处理异常，此层面直接被controller调用，面对BizData
		
		try {
			JSONObject jsonTable = dao.query(param);
			
			JSONObject json = new JSONObject();
			json.put("code", 0);
			json.put("msg", "success");
			json.put("data", jsonTable);
			
			return json;
		} catch (SQLException e) {
			e.printStackTrace();
			
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("msg", e.getMessage());
			return json;
		}
	}

	@Override
	public JSONObject validData(JSONObject param) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//更新期初
	@Override
	public PeriodCaption insert(
			String yearNo
			, String periodNo
			
			, String captionID
			, String captionNo
			, String captionName
			, String captionFullName
			
			, String billType
			, String billTypeName 
			
			, int dob//余额方向
			
			, String currency//币种
			, String currencyLabel//币种
			
			, int nodeLevel
			, String nodeKind 
			, String sortNo
			, String fParent

			, String userId
			, String enterpriseNo
			, String enterpriseName
			){
		//首先判断是否存在记录，如果不存在就创建
		
		PeriodCaption obj = new PeriodCaption();
		
		//不存在，新建
		obj.setfID(Utils.getUUID());
		
		obj.setYearNo(yearNo);
		obj.setPeriodNo(periodNo);
		
		obj.setCaptionID(captionID);
		obj.setCaptionNo(captionNo);
		obj.setCaptionName(captionName);
		obj.setCaptionFullName(captionFullName);
		
		obj.setBillType(billType);
		obj.setBillTypeName(billTypeName);
		
		obj.setNodeLevel(nodeLevel);
		obj.setNodeKind(nodeKind);
		obj.setSortNo(sortNo);
		obj.setfParent(fParent);
		
		obj.setDob(dob);
		
		obj.setCurrency(currency);
		obj.setCurrencyLabel(currencyLabel);
		
		obj.setDebitBegin(BigDecimal.ZERO);
		obj.setCreditBegin(BigDecimal.ZERO);
		
		obj.setDebitPeriod(BigDecimal.ZERO);
		obj.setCreditPeriod(BigDecimal.ZERO);
		
		obj.setDebitCount(0);
		obj.setCreditCount(0);
		
		obj.setDebitCountYear(0);
		obj.setCreditCountYear(0);
		
		obj.setDebitYear(BigDecimal.ZERO);
		obj.setCreditYear(BigDecimal.ZERO);
		
		obj.setDebitEnd(BigDecimal.ZERO);
		obj.setCreditEnd(BigDecimal.ZERO);
		
		obj.setDebitYearBegin(BigDecimal.ZERO);
		obj.setCreditYearBegin(BigDecimal.ZERO);
		obj.setBalanceYearBegin(BigDecimal.ZERO);
		obj.setBalanceBegin(BigDecimal.ZERO);
		obj.setBalanceEnd(BigDecimal.ZERO);
		
		obj.setDebitYearEnd(BigDecimal.ZERO);
		obj.setCreditYearEnd(BigDecimal.ZERO);
		obj.setBalanceYearEnd(BigDecimal.ZERO);
		
		obj.setUserId(userId);
		obj.setEnterpriseNo(enterpriseNo);
		obj.setEnterpriseName(enterpriseName);
		obj.setVersion(0);
		
		dao.insert(obj);
		
		return obj;
	}

	//结转（这种方案不能使得会计科目档案中新增的会计科目进入月档）
	/*
	@Override
	public int next(String yearNo, String periodNo, String yearNoNext, String periodNoNext, String userId, String enterpriseNo){
		//获取本期
		List<PeriodCaption> list = dao.getByPeriod(yearNo, periodNo, enterpriseNo);
		for(PeriodCaption obj: list) {
			String captionID = obj.getCaptionID();
			String captionNo = obj.getCaptionNo();
			String captionName = obj.getCaptionName();
			String captionFullName = obj.getCaptionFullName();

			String billType = obj.getBillType();
			String billTypeName = obj.getBillTypeName();
			
			int dob = obj.getDob();//余额方向
			String dobLabel = obj.getDobLabel();//余额方向
			
			String currency = obj.getCurrency();//币种
			String currencyLabel = obj.getCurrencyLabel();//币种
			
			int nodeLevel = obj.getNodeLevel();
			String nodeKind = obj.getNodeKind();
			String sortNo = obj.getSortNo();
			String fParent = obj.getfParent();

			//年初
			BigDecimal debitYearBegin = obj.getDebitYearBegin();
			BigDecimal creditYearBegin = obj.getCreditYearBegin();
			BigDecimal balanceYearBegin = obj.getBalanceYearBegin();
			//本年累计
			BigDecimal debitYear = obj.getDebitYear();
			BigDecimal creditYear = obj.getCreditYear();
			int debitCountYear = obj.getDebitCountYear();
			int creditCountYear = obj.getCreditCountYear();
			//年末
			BigDecimal debitYearEnd = obj.getDebitYearEnd();
			BigDecimal creditYearEnd = obj.getCreditYearEnd();
			BigDecimal balanceYearEnd = obj.getBalanceYearEnd();

			//期初
			BigDecimal debitBegin = obj.getDebitEnd();
			BigDecimal creditBegin = obj.getCreditEnd();
			BigDecimal balanceBegin = obj.getBalanceEnd();

			String enterpriseName = obj.getEnterpriseName();
			
			//生成下期
			PeriodCaption periodCaption = this.insert(yearNoNext, periodNoNext, captionID, captionNo, captionName, captionFullName, billType, billTypeName, dob, dobLabel, currency, currencyLabel, nodeLevel, nodeKind, sortNo, fParent, userId, enterpriseNo, enterpriseName);
			//续写本年期初
			periodCaption.setDebitYearBegin(debitYearBegin);
			periodCaption.setCreditYearBegin(creditYearBegin);
			periodCaption.setBalanceYearBegin(balanceYearBegin);
			
			//续写本年累计
			periodCaption.setDebitYear(debitYear);
			periodCaption.setCreditYear(creditYear);
			periodCaption.setDebitCountYear(debitCountYear);
			periodCaption.setCreditCountYear(creditCountYear);
			//续写本年期末
			periodCaption.setDebitYearEnd(debitYearEnd);
			periodCaption.setCreditYearEnd(creditYearEnd);
			periodCaption.setBalanceYearEnd(balanceYearEnd);
			
			//结转期初
			periodCaption.setDebitBegin(debitBegin);
			periodCaption.setCreditBegin(creditBegin);
			periodCaption.setBalanceBegin(balanceBegin);
			//同时写入期末
			periodCaption.setDebitEnd(debitBegin);
			periodCaption.setCreditEnd(creditBegin);
			periodCaption.setBalanceEnd(balanceBegin);
			
			dao.update(periodCaption);
		}
		return 0;
	}
	*/
	
	@Override
	public PeriodCaption calculate(PeriodCaption obj){
		int dob = obj.getDob();//余额方向
		
		//根据分配完成的数据，计算结果（在撤销函数中保持不变）
		//////////////////////////////////////////////////////////
		//年度
		BigDecimal debitYearEnd = obj.getDebitYearBegin().add(obj.getDebitYear());
		obj.setDebitYearEnd(debitYearEnd);
		
		BigDecimal creditYearEnd = obj.getCreditYearBegin().add(obj.getCreditYear());
		obj.setCreditYearEnd(creditYearEnd);

		BigDecimal balanceYearEnd = obj.getDebitYearEnd().subtract(obj.getCreditYearEnd());
		balanceYearEnd = balanceYearEnd.multiply(new BigDecimal(dob)).setScale(2, BigDecimal.ROUND_HALF_UP);
		obj.setBalanceYearEnd(balanceYearEnd);
		//本期
		BigDecimal debitEnd = obj.getDebitBegin().add(obj.getDebitPeriod());
		obj.setDebitEnd(debitEnd);

		BigDecimal creditEnd = obj.getCreditBegin().add(obj.getCreditPeriod());
		obj.setCreditEnd(creditEnd);

		BigDecimal balanceEnd = obj.getDebitEnd().subtract(obj.getCreditEnd());
		balanceEnd = balanceEnd.multiply(new BigDecimal(dob)).setScale(2, BigDecimal.ROUND_HALF_UP);
		obj.setBalanceEnd(balanceEnd);
		/////////////////////////////////////////////////////////////
		
		return obj;
	}
	
	//结束开账
	@Override
	public int initSubmit(String yearNo, String periodNo, String enterpriseNo){
		//获取本期
		List<PeriodCaption> list = dao.getByPeriod(yearNo, periodNo, enterpriseNo);
		for(PeriodCaption obj: list) {
			int dob = obj.getDob();//余额方向
			//分配年初余额到关联列
			//年初余额：需要处理年初借方和年初贷方余额
			//balanceYearBegin = (debitYearBegin - creditYearBegin)*direction
			//计算参数设置（在撤销函数中计算符号相反）
			//放在前端处理
			/*
			BigDecimal balanceYearBegin = obj.getBalanceYearBegin();
			BigDecimal balanceBegin = obj.getBalanceBegin();
			if(dob>0){
				//借方
				obj.setDebitYearBegin(obj.getDebitYearBegin().add(balanceYearBegin));
				obj.setDebitBegin(obj.getDebitBegin().add(balanceBegin));
			}
			if(dob<0){
				//贷方
				obj.setCreditYearBegin(obj.getCreditYearBegin().add(balanceYearBegin));
				obj.setCreditBegin(obj.getCreditBegin().add(balanceBegin));
			}
			*/
			//根据分配完成的数据，计算结果（在撤销函数中保持不变）
			
			//////////////////////////////////////////////////////////
			//年度
			BigDecimal debitYearEnd = obj.getDebitYearBegin().add(obj.getDebitYear());
			obj.setDebitYearEnd(obj.getDebitYearEnd().add(debitYearEnd));
			
			BigDecimal creditYearEnd = obj.getCreditYearBegin().add(obj.getCreditYear());
			obj.setCreditYearEnd(obj.getCreditYearEnd().add(creditYearEnd));

			BigDecimal balanceYearEnd = obj.getDebitYearEnd().subtract(obj.getCreditYearEnd());
			balanceYearEnd = balanceYearEnd.multiply(new BigDecimal(dob)).setScale(2, BigDecimal.ROUND_HALF_UP);
			obj.setBalanceYearEnd(balanceYearEnd);
			//本期
			BigDecimal debitEnd = obj.getDebitBegin().add(obj.getDebitPeriod());
			obj.setDebitEnd(obj.getDebitEnd().add(debitEnd));

			BigDecimal creditEnd = obj.getCreditBegin().add(obj.getCreditPeriod());
			obj.setCreditEnd(obj.getCreditEnd().add(creditEnd));

			BigDecimal balanceEnd = obj.getDebitEnd().subtract(obj.getCreditEnd());
			balanceEnd = balanceEnd.multiply(new BigDecimal(dob)).setScale(2, BigDecimal.ROUND_HALF_UP);
			obj.setBalanceEnd(balanceEnd);
			/////////////////////////////////////////////////////////////
			//下面的计算方法直接设置期末余额（不可取）
			//this.calculate(obj);
			
			dao.update(obj);
		}
		
		return 0;
	}
	
	//取消结束
	@Override
	public int initActive(String yearNo, String periodNo, String enterpriseNo){
		//获取本期
		List<PeriodCaption> list = dao.getByPeriod(yearNo, periodNo, enterpriseNo);
		for(PeriodCaption obj: list) {
			int dob = obj.getDob();//余额方向
			//分配年初余额到关联列
			//年初余额：需要处理年初借方和年初贷方余额
			//balanceYearBegin = (debitYearBegin - creditYearBegin)*direction
			//计算参数设置（在撤销函数中计算符号相反）
			//放在前端处理
			/*
			BigDecimal balanceYearBegin = obj.getBalanceYearBegin();
			BigDecimal balanceBegin = obj.getBalanceBegin();
			if(dob>0){
				//借方
				obj.setDebitYearBegin(obj.getDebitYearBegin().subtract(balanceYearBegin));
				obj.setDebitBegin(obj.getDebitBegin().subtract(balanceBegin));
			}
			if(dob<0){
				//贷方
				obj.setCreditYearBegin(obj.getCreditYearBegin().subtract(balanceYearBegin));
				obj.setCreditBegin(obj.getCreditBegin().subtract(balanceBegin));
			}
			*/
			//根据分配完成的数据，计算结果（在撤销函数中保持不变）
			
			//////////////////////////////////////////////////////////
			//年度
			BigDecimal debitYearEnd = obj.getDebitYearBegin().add(obj.getDebitYear());
			obj.setDebitYearEnd(obj.getDebitYearEnd().subtract(debitYearEnd));
			
			BigDecimal creditYearEnd = obj.getCreditYearBegin().add(obj.getCreditYear());
			obj.setCreditYearEnd(obj.getCreditYearEnd().subtract(creditYearEnd));

			BigDecimal balanceYearEnd = obj.getDebitYearEnd().subtract(obj.getCreditYearEnd());
			balanceYearEnd = balanceYearEnd.multiply(new BigDecimal(dob)).setScale(2, BigDecimal.ROUND_HALF_UP);
			obj.setBalanceYearEnd(balanceYearEnd);
			//本期
			BigDecimal debitEnd = obj.getDebitBegin().add(obj.getDebitPeriod());
			obj.setDebitEnd(obj.getDebitEnd().subtract(debitEnd));

			BigDecimal creditEnd = obj.getCreditBegin().add(obj.getCreditPeriod());
			obj.setCreditEnd(obj.getCreditEnd().subtract(creditEnd));

			BigDecimal balanceEnd = obj.getDebitEnd().subtract(obj.getCreditEnd());
			balanceEnd = balanceEnd.multiply(new BigDecimal(dob)).setScale(2, BigDecimal.ROUND_HALF_UP);
			obj.setBalanceEnd(balanceEnd);
			/////////////////////////////////////////////////////////////
			//下面的计算方法直接设置期末余额（不可取）
			//this.calculate(obj);
			
			dao.update(obj);
		}
		
		return 0;
	}
	
	//结束开账后试算平衡
	@Override
	public JSONObject initTrial(String yearNo, String periodNo, String enterpriseNo){
		//获取本期
		JSONObject json = new JSONObject();
		
		//汇总信息
		//期初余额借方
		BigDecimal debitBegin = BigDecimal.ZERO;
		//期初余额贷方
		BigDecimal creditBegin = BigDecimal.ZERO;
		//本年累计借方
		BigDecimal debitYear = BigDecimal.ZERO;
		//本年累计贷方
		BigDecimal creditYear = BigDecimal.ZERO;
		//年初余额借方
		BigDecimal debitYearBegin = BigDecimal.ZERO;
		//年初余额贷方
		BigDecimal creditYearBegin = BigDecimal.ZERO;
		
		
		//详细信息
		//资产
		BigDecimal zcPeriodBegin = BigDecimal.ZERO;
		BigDecimal zcYearBegin = BigDecimal.ZERO;
		//负债
		BigDecimal fzPeriodBegin = BigDecimal.ZERO;
		BigDecimal fzYearBegin = BigDecimal.ZERO;
		//所有者权益
		BigDecimal qyPeriodBegin = BigDecimal.ZERO;
		BigDecimal qyYearBegin = BigDecimal.ZERO;
		//成本
		BigDecimal cbPeriodBegin = BigDecimal.ZERO;
		BigDecimal cbYearBegin = BigDecimal.ZERO;
		//损益
		BigDecimal syPeriodBegin = BigDecimal.ZERO;
		BigDecimal syYearBegin = BigDecimal.ZERO;
		
		
		
		List<PeriodCaption> list = dao.getByPeriod(yearNo, periodNo, enterpriseNo);
		for(PeriodCaption obj: list) {
			//只需汇总顶级科目即可
			if(obj.getNodeLevel()>1) {
				continue;
			}
			//汇总信息
			debitBegin = debitBegin.add(obj.getDebitBegin());
			creditBegin = creditBegin.add(obj.getCreditBegin());
			debitYear = debitYear.add(obj.getDebitYear());
			creditYear = creditYear.add(obj.getCreditYear());
			debitYearBegin = debitYearBegin.add(obj.getDebitYearBegin());
			creditYearBegin = creditYearBegin.add(obj.getCreditYearBegin());
			//详细信息
			
			switch(obj.getBillType()) {
				case "ZCKM":
					zcPeriodBegin = zcPeriodBegin.add(obj.getBalanceBegin());
					zcYearBegin = zcYearBegin.add(obj.getBalanceYearBegin());
					break;
				case "FZKM":
					fzPeriodBegin = fzPeriodBegin.add(obj.getBalanceBegin());
					fzYearBegin = fzYearBegin.add(obj.getBalanceYearBegin());
					break;
				case "QYKM":
					qyPeriodBegin = qyPeriodBegin.add(obj.getBalanceBegin());
					qyYearBegin = qyYearBegin.add(obj.getBalanceYearBegin());
					break;
				case "CBKM":
					cbPeriodBegin = cbPeriodBegin.add(obj.getBalanceBegin());
					cbYearBegin = cbYearBegin.add(obj.getBalanceYearBegin());
					break;
				case "SYKM":
					syPeriodBegin = syPeriodBegin.add(obj.getBalanceBegin());
					syYearBegin = syYearBegin.add(obj.getBalanceYearBegin());
					break;
				default:
					System.out.println("未参与统计的科目："+obj.getBillTypeName());
			}
		}
		
		//打包汇总信息
		JSONArray totalRows = new JSONArray();
		JSONObject row = new JSONObject();
		row.put("debit", debitBegin);
		row.put("credit", creditBegin);
		totalRows.add(row);
		row = new JSONObject();
		row.put("debit", debitYear);
		row.put("credit", creditYear);
		totalRows.add(row);
		row = new JSONObject();
		row.put("debit", debitYearBegin);
		row.put("credit", creditYearBegin);
		totalRows.add(row);

		//详细信息
		JSONArray beginRows = new JSONArray();
		row = new JSONObject();
		row.put("periodBegin", zcPeriodBegin);
		row.put("yearBegin", zcYearBegin);
		beginRows.add(row);
		row = new JSONObject();
		row.put("periodBegin", fzPeriodBegin);
		row.put("yearBegin", fzYearBegin);
		beginRows.add(row);
		row = new JSONObject();
		row.put("periodBegin", qyPeriodBegin);
		row.put("yearBegin", qyYearBegin);
		beginRows.add(row);
		row = new JSONObject();
		row.put("periodBegin", cbPeriodBegin);
		row.put("yearBegin", cbYearBegin);
		beginRows.add(row);
		row = new JSONObject();
		row.put("periodBegin", syPeriodBegin);
		row.put("yearBegin", syYearBegin);
		beginRows.add(row);

		
		
		JSONObject data = new JSONObject();
		data.put("totalRows",totalRows);
		data.put("beginRows",beginRows);

		json.put("code",0);
		json.put("msg","success");
		json.put("data",data);
		
		return json;
	}
	
	//会计月结
	@Transactional
	@Override
	public int next(String yearNo, String periodNo, String yearNoNext, String periodNoNext, String userId, String enterpriseNo){

		//获取
		List<PeriodCaption> list = dao.getByPeriod(yearNo, periodNo, enterpriseNo);
		for(PeriodCaption obj: list) {
//		List<AccountCaption> list = accountCaptionService.getList(enterpriseNo);
//		for(AccountCaption accountCaption: list) {
//			String captionNo = accountCaption.getCaptionNo();
//			//获取当期数据
//			PeriodCaption obj = periodCaptionService.getByNo(yearNo, periodNo, captionNo, enterpriseNo);
			//循环写入下期月档
//			PeriodCaption periodCaption = this.captionIntoPeriod(yearNoNext, periodNoNext, accountCaption, userId, enterpriseNo);
			
			String captionNo = obj.getCaptionNo();
			String captionID = obj.getfID();
			String captionName = obj.getCaptionName();
			String captionFullName = obj.getCaptionFullName();
			
			String billType = obj.getBillType();
			String billTypeName = obj.getBillTypeName();
			
			int dob = obj.getDob();//余额方向
			
			String currency = obj.getCurrency();
			String currencyLabel = obj.getCurrencyLabel();
			
			int nodeLevel = obj.getNodeLevel();
			String nodeKind = obj.getNodeKind();
			String sortNo = obj.getSortNo();
			String fParent = obj.getfParent();

			String enterpriseName = obj.getEnterpriseName();
			
			PeriodCaption periodCaption = this.insert(yearNoNext, periodNoNext, captionID, captionNo, captionName, captionFullName, billType, billTypeName, dob, currency, currencyLabel, nodeLevel, nodeKind, sortNo, fParent, userId, enterpriseNo, enterpriseName);

			if("12".equals(periodNo)) {
				//如果当前是12月份，就是年底

				///////////年度////////////////
				//年初
				BigDecimal debitYearBegin = obj.getDebitYearEnd();
				BigDecimal creditYearBegin = obj.getCreditYearEnd();
				BigDecimal balanceYearBegin = obj.getBalanceYearEnd();
				
				//---------------------------------------------------
				
				//续写本年期初
				periodCaption.setDebitYearBegin(debitYearBegin);
				periodCaption.setCreditYearBegin(creditYearBegin);
				periodCaption.setBalanceYearBegin(balanceYearBegin);
				//续写本年期末
				periodCaption.setDebitYearEnd(debitYearBegin);
				periodCaption.setCreditYearEnd(debitYearBegin);
				periodCaption.setBalanceYearEnd(debitYearBegin);
				////////////////////////////////
					
			}else {

				///////////年度////////////////
				//年初
				BigDecimal debitYearBegin = obj.getDebitYearBegin();
				BigDecimal creditYearBegin = obj.getCreditYearBegin();
				BigDecimal balanceYearBegin = obj.getBalanceYearBegin();
				//本年累计
				BigDecimal debitYear = obj.getDebitYear();
				BigDecimal creditYear = obj.getCreditYear();
				int debitCountYear = obj.getDebitCountYear();
				int creditCountYear = obj.getCreditCountYear();
				//年末
				BigDecimal debitYearEnd = obj.getDebitYearEnd();
				BigDecimal creditYearEnd = obj.getCreditYearEnd();
				BigDecimal balanceYearEnd = obj.getBalanceYearEnd();
				
				//---------------------------------------------------
				
				//续写本年期初
				periodCaption.setDebitYearBegin(debitYearBegin);
				periodCaption.setCreditYearBegin(creditYearBegin);
				periodCaption.setBalanceYearBegin(balanceYearBegin);
				//续写本年累计
				periodCaption.setDebitYear(debitYear);
				periodCaption.setCreditYear(creditYear);
				periodCaption.setDebitCountYear(debitCountYear);
				periodCaption.setCreditCountYear(creditCountYear);
				//续写本年期末
				periodCaption.setDebitYearEnd(debitYearEnd);
				periodCaption.setCreditYearEnd(creditYearEnd);
				periodCaption.setBalanceYearEnd(balanceYearEnd);
				////////////////////////////////
				
			}

			///////////月档/////////////////////////////
			//期初
			BigDecimal debitBegin = obj.getDebitEnd();
			BigDecimal creditBegin = obj.getCreditEnd();
			BigDecimal balanceBegin = obj.getBalanceEnd();
			
			//-------------------------------------------------
			
			//结转期初
			periodCaption.setDebitBegin(debitBegin);
			periodCaption.setCreditBegin(creditBegin);
			periodCaption.setBalanceBegin(balanceBegin);
			//同时写入期末
			periodCaption.setDebitEnd(debitBegin);
			periodCaption.setCreditEnd(creditBegin);
			periodCaption.setBalanceEnd(balanceBegin);
			///////////////////////////////////////////////
			
			dao.update(periodCaption);
		}
		
		return 0;
	}

	
}
